//
//  SetBeautyView.m
//  TILLiveSDKShow
//
//  Created by wilderliao on 16/11/11.
//  Copyright © 2016年 Tencent. All rights reserved.
//

#import "SetBeautyView.h"
#import "UIView+CustomAutoLayout.h"




@implementation SetBeautyView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addOwnViews];
        [self layoutViews];
    }
    return self;
}

- (void)addOwnViews{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    
    _clearBg = [[UIView alloc] init];
    [self addSubview:_clearBg];
    _buttonArray = [[NSMutableArray alloc] init];
    _sliderBack = [[UIView alloc] init];
    _sliderBack.backgroundColor = [Theme_Color_Black colorWithAlphaComponent:0.3];
    _sliderBack.clipsToBounds = NO;
    [self addSubview:_sliderBack];
    
    UILabel *levellabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, KScreenWidth, 40)];
    levellabel.font = FontP(15);
    levellabel.textColor = [UIColor whiteColor];
    levellabel.textAlignment = NSTextAlignmentCenter;
    levellabel.text = @"美颜级别";
    [_sliderBack addSubview:levellabel];
    

    CGFloat btnW = (KScreenWidth-110*SCREEN_WIDTH_RATIO47)/6;
    CGFloat btnY = levellabel.bottom+17.5*SCREEN_WIDTH_RATIO47;
    CGFloat btnK = 15*SCREEN_WIDTH_RATIO47;
    
    for (int i = 0 ; i< 6; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        button.frame = CGRectMake(btnK+(i*(btnW+btnK+1)), btnY, btnW, btnW);

        button.layer.masksToBounds=YES;
        button.layer.cornerRadius=btnK/2;
        [button addTarget:self action:@selector(buttonBeautyClick:) forControlEvents:UIControlEventTouchUpInside];

        if (i == 0) {
            [button setTitle:@"" forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"beauty_not_pressed_button"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"beauty_pressed_button"] forState:UIControlStateSelected];
            
         
        }else{
            [button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"beauty_level_not_pressed_button"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"beauty_level_pressed_button"] forState:UIControlStateSelected];
        }
        
        [_buttonArray addObject:button];
        [_sliderBack addSubview:button];
    }
    




    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [_clearBg addGestureRecognizer:tap];
}

- (void)buttonBeautyClick:(UIButton *)sender{
    
    
    for (int i=0;i<6;i++){
        
        UIButton *btn = _buttonArray[i];
        
        if(btn.tag != sender.tag){
            btn.selected=NO;
        }else {
            
            btn.selected =! btn.selected;
        }
    }
  
    switch (sender.tag) {
        case 0:{
            _beautyFloat = 0.0;
            _changeCompletion(BeautyViewType_Beauty, _beautyFloat);

        }
            break;
        case 1:{
            _beautyFloat = 0.2;
            _changeCompletion(BeautyViewType_Beauty, _beautyFloat);

        }
            break;
        case 2:{
            _beautyFloat = 0.4;
            _changeCompletion(BeautyViewType_Beauty, _beautyFloat);

        }
            break;
        case 3:{
            _beautyFloat = 0.6;
            _changeCompletion(BeautyViewType_Beauty, _beautyFloat);

        }
            break;
        case 4:{
            _beautyFloat = 0.8;
            _changeCompletion(BeautyViewType_Beauty, _beautyFloat);

            
        }
            break;
        case 5:{
            _beautyFloat = 1.0;
            _changeCompletion(BeautyViewType_Beauty, _beautyFloat);

        }
            break;
               default:
            break;
    }
 
}
- (void)layoutViews
{
    CGRect selfRect = self.bounds;
    
    _clearBg.frame = selfRect;
    
    [_sliderBack sizeWith:CGSizeMake(selfRect.size.width, 170)];
    [_sliderBack alignParentBottom];


}

- (void)onTap:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.7 animations:^{
            CGRect selfRect = self.frame;
            selfRect.origin.y += selfRect.size.height;
            [self setFrame:selfRect];
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}

- (void)setBeautyValue:(CGFloat)beauty{
    _beautyFloat = beauty;

}

@end
